package com.whfp.coky.ui.base

import androidx.viewbinding.ViewBinding
import com.whfp.coky.R
import com.whfp.coky.databinding.ActivityRfidBinding
import com.whfp.coky.databinding.RfidItemBinding
import com.whfp.coky.event.EventCode
import com.whfp.coky.event.EventMessage
import com.whfp.coky.other.Constants
import com.whfp.coky.ui.base.vm.RFIDVM
import com.whfp.coky.ui.task.model.CheckDeviceBean
import com.whfp.coky.widget.clicks

/**
 *
 * rfid基类,加载rfid功能
 * author coky
 * created 2022/3/31 12:43
 */

abstract class RFIDAct<VM : RFIDVM> : BaseActivity<VM, ActivityRfidBinding>() {

    lateinit var stubBinding:ViewBinding//区分界面加载


    override fun initView() {
        baseTips()
        vModel.regRFID()
        vBinding.sbConn.getRightView().clicks {
            vModel.connBtnStatusChange()
        }

//        还有一个提交click，在loadview中加载LL
    }

    abstract fun baseTips()//基类tips文字赋值

    open fun loadView(){//加载stub,默认显示二维码相关
//        vBinding.vs.layoutResource = R.layout.rfid_item
//        stubBinding = RfidItemBinding.bind(vBinding.vs.inflate())
//        (stubBinding as RfidItemBinding).tvBqnr.text = vModel.getTagContent()
//        (stubBinding as RfidItemBinding).tvXhqd.text = vModel.getSignPower()
//        (stubBinding as RfidItemBinding).btnRwtj.clicks {
//            onCommitClicked()
//        }

//        代码改造
        vBinding.vs.layoutResource = R.layout.rfid_item
        RfidItemBinding.bind(vBinding.vs.inflate()).run {
            tvBqnr.text = vModel.getTagContent()
            tvXhqd.text = vModel.getSignPower()
            btnRwtj.clicks {
                onCommitClicked()
            }
        }

    }

    open fun onCommitClicked(){}//stub加载后点击事件

    override fun onDestroy() {
        vModel.unRegRFID()
        super.onDestroy()
    }

    override fun handleEvent(msg: EventMessage) {
        super.handleEvent(msg)
        when(msg.code){
            EventCode.SCAN_CODE -> {
                toast("RFIDAct收到结果${msg.msg}")
                vModel.doVerDevice(msg.msg)
            }
        }
    }

//    处理校验的设备
    override fun handleDevice(bean: CheckDeviceBean) {
        super.handleDevice(bean)
        bean.let {
            if (it.deviceMac !== null && it.refType.equals(Constants.DEV_TYPE_RFID)) {
                showDialog("设备连接中，请稍后...")
                vModel.myRFID.connect(it.deviceMac, 10000)
            } else {
                toast("设备型号错误！")
            }
        }
    }


}